﻿using NB.MGJ.Common;
using NB.MGJ.Common.Helper;
using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace NB.MGJ.Web.Controllers {
	public partial class ManagerController : BaseController {
		private CommonService _comservice;
		private ManagerService _mservice;
		private JsonHelper jsonHelper = new JsonHelper();
		private HtmlHelperService _hhservice;
        private ProductService _productService;

		public ManagerController() {
			_mservice = new ManagerService();
			_hhservice = new HtmlHelperService();
			_comservice = new CommonService();
            _productService = new ProductService();
		}

		#region 相关测试页面

		/// <summary>
		/// 地图首页
		/// </summary>
		/// <returns></returns>
        public ActionResult Index()
        {
            /*有用的代码
             *按钮权限过滤
             *推送消息接收过滤
             */
            try
            {
                ViewBag.EnterpriseId = LogOnUser.EnterpriseId;
                ViewBag.UserId = LogOnUser.UserID;
            }
            catch { }
            if (LogOnUser.EnterpriseId == 1)
            {
                return View("AdminIndex");
            }
            return View();
        }

        public ActionResult AdminIndex()
        {
            /*有用的代码
             *按钮权限过滤
             *推送消息接收过滤
             */
            try
            {
                ViewBag.EnterpriseId = LogOnUser.EnterpriseId;
                ViewBag.UserId = LogOnUser.UserID;
            }
            catch { }
            return View();
        }
		public ActionResult Test() {
			return View();
		}

		public ActionResult Test_1() {
			return View();
		}

		public ActionResult Test_2() {
			return View();
		}
		#endregion
	
		#region 权限管理
		public ActionResult PrivilegeManage() {
			return View();
		}
		public ActionResult AddPrivilege() {
			return View();
		}
		public ActionResult EditPrivilege() {
			return View();
		}
		#endregion

		#region 右侧盒子模型
		public ActionResult RightFlex() {
			return View();
		}
		#endregion

		#region 片区管理

		public ActionResult AreaManage() {
			return View();
		}
		#endregion

		#region 经销商等级管理

		public ActionResult DealerLevel() {
			return View();
		}

        /// <summary>
        /// Pass 1
        /// </summary>
        public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
                L.EntityModel.AntiFleeing_DistributorLevel d = _mservice.GetLevelModelId(id.Value);
                return View(d);
            }
            return View();
        }

        public int leavel()
        {
            var result = _mservice.leavel(LogOnUser.EnterpriseId);
            return result;
        }
        //Pass 1
        public JsonResult CheckDealerLevelName(string LevelName)
        {
            var result = _mservice.GetDealerLevelName(LevelName, LogOnUser.EnterpriseId);
            return Json(new { valid = result });
        }
        public JsonResult CheckDealerLevel(string Level)
        {
            var result = _mservice.GetDealerLevel(Level, LogOnUser.EnterpriseId);
            return Json(new { valid = result });
        }
		/// <summary>
		/// 导出EXCEL
        /// Pass 1
		/// </summary>
		public ActionResult ExportExecl(string key) {
			var model = _mservice.GetAllLevel(key);
			var _byte_batch = Excel.Exprot<AntiFleeing_DistributorLevel>(model.ToList().Where(p => p.ViewDel == 0).ToList(), new string[] { "等级", "等级名称", "操作时间" }, p => {
				return new object[] { 
                            p.Level,
                            p.LevelName,
                            p.ChangeDate,               
				};
			}, "DealerLevel");
			return File(_byte_batch, "application/octet-stream", string.Format("{0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
		}
		
        /// <summary>
		/// 分页数据
		/// </summary>
		public JsonResult DealerLevelList(string dsearch, int pageIndex = 1, int pageSize = 10) {
			PagerModel pm = new PagerModel();
			pm.Page = pageIndex;
			pm.PageSize = pageSize;
           string strWhere="";
           if (LogOnUser.EnterpriseId != 1)
           {
               if (string.IsNullOrEmpty(LogOnUser.JuniorEnterprise))
               { strWhere = " and EnterpriseId  in (0)"; }
               else
               { strWhere = " and EnterpriseId in (" + LogOnUser.JuniorEnterprise + ")"; }
           }
           else { strWhere = " and 1=1"; }
            
			pm.StrCondition = dsearch != null ? " and  LevelName like '%" + dsearch + "%' " : dsearch;
            pm.StrCondition=strWhere+ pm.StrCondition;
			var model = _mservice.GetDealerLevelService(ref pm);
		
			return Json(model);
		}
		
		public int AddLevelName(AntiFleeing_DistributorLevel model) {
			model.CreateDate = DateTime.Now;
			model.ChangeDate = DateTime.Now;
			model.ChangeLoginID = LogOnUser.UserID;
			model.ChangeLoginName = LogOnUser.LoginName;
            model.EnterpriseId = LogOnUser.EnterpriseId;
			model.ViewDel = 0;
			return _mservice.AddLevel(model);
		}
		public int EditLeve(AntiFleeing_DistributorLevel model,int id) {	
			DBHController dbh = new DBHController();
			model.Id = id;
			model.ChangeDate = DateTime.Now;
			model.ChangeLoginID = LogOnUser.UserID;
			model.ChangeLoginName = LogOnUser.LoginName;
            model.CreateDate = DateTime.Now;
			model.ViewDel = 0;
            model.EnterpriseId = LogOnUser.EnterpriseId;
			int rulset = _mservice.EditLeavel(model); 
			return rulset;
		}
	
		public int DelLeve(int id) {
			int result = _mservice.DelDealerLevelService(id);
			return result;
		}
	
	}
		#endregion

}

	
